package com.sansi.pencilbook.controller;

import com.sansi.pencilbook.DO.ResponseDO;
import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.Category;
import com.sansi.pencilbook.entity.User;
import com.sansi.pencilbook.service.UserServer;
import com.sansi.pencilbook.util.ResponseDOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 功能说明: 用户控制器
 *
 * @author lsy13
 */
@Slf4j
@RestController
@RequestMapping(("/api/user"))
public class UserController extends VerifyController implements BaseController{

    @Autowired
    private UserServer userServer;

    @PostMapping("/v/register")
    public ResponseDO register(@RequestBody User user){
        log.info("用户注册");
        Result result = userServer.register(user);
        return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }

    @PostMapping("/v/login")
    public ResponseDO login(@RequestParam("userAccount") String userAccount, @RequestParam("userPassword") String userPassword) {
        log.info("用户登录");
        Result result = userServer.login(userAccount, userPassword);
        return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }

    @PostMapping("/u/modifyUser")
    public ResponseDO modify(@RequestBody User user, HttpServletRequest request) {
        log.info("修改信息");
        if (checkUser(request) || checkAdmin(request)) {
            User user1 = (User) userServer.getUserByAccount(user.getUserAccount()).getData();
            String userPassword = null;
            if(user.getUserPassword().indexOf("_")>0) {
                userPassword = user.getUserPassword().split("_")[1];
                user.setUserPassword(user.getUserPassword().split("_")[0]);
            }else {
                userPassword = user.getUserPassword();
            }
            if (user1.getUserPassword().equals(userPassword)) {
                Result result = userServer.modifyUser(user);
                return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
            }else {
                return ResponseDOUtil.setResponseDO(false, "密码错误");
            }
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
        }
    }

    @GetMapping("/u/getUserByAccount")
    public ResponseDO getUserInfo(HttpServletRequest request) {
        log.info("获取用户信息");
        Cookie[] cookies = request.getCookies();
        String userAccount = null;
        for (Cookie cookie : cookies) {
            if ("userAccount".equals(cookie.getName())) {
                userAccount = cookie.getValue();
            }
        }
        if (checkUser(userAccount)) {
            Result result = userServer.getUserByAccount(userAccount);
            return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData());
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
        }
    }

    @GetMapping("/a/getAllUsers")
    public ResponseDO getAllUsers(HttpServletRequest request) {
        if (checkAdmin(request)) {
            Result result = userServer.getAllUsers();
            return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
                    ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
        }
    }

    @PostMapping("/u/deleteUser")
    public ResponseDO deleteUser(@RequestBody User user, HttpServletRequest request) {
        log.info("删除用户");
        if (checkUser(request) || checkAdmin(request)) {
            Result result = userServer.deleteUser(user);
            return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
        }
    }

    @PostMapping("/u/deleteUsers")
    public ResponseDO deleteUsers(@RequestBody List<User> users, HttpServletRequest request) {
        log.info("批量删除用户");
        if (checkUser(request) || checkAdmin(request)) {
            Result result = userServer.deleteUsers(users);
            return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        }else {
            return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
        }
    }
}
